package com.huiyuanai.servlet;

import com.alibaba.fastjson.JSON;
import com.huiyuanai.dao.Contact;
import com.huiyuanai.dao.Provider;
import com.huiyuanai.dao.User;
import com.huiyuanai.service.bill.BillService;
import com.huiyuanai.service.bill.BillServiceImpl;
import com.huiyuanai.service.provider.ProviderService;
import com.huiyuanai.service.provider.ProviderServiceImpl;
import com.huiyuanai.util.Constans;
import com.mysql.cj.util.StringUtils;
import lombok.SneakyThrows;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.math.BigDecimal;
import java.util.*;

/**
 * @Author: liwan
 * @Description:
 * @Date: create in 2021/10/18
 */
public class BillServlet extends HttpServlet {
    @SneakyThrows
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String method = req.getParameter("method");
        if (!StringUtils.isNullOrEmpty(method) && "query".equals(method)) {
            query(req, resp);
        }
        if (!StringUtils.isNullOrEmpty(method) && "add".equals(method)) {
            add(req, resp);
        }
        if (!StringUtils.isNullOrEmpty(method) && "getproviderlist".equals(method)) {
            getProviderList(req, resp);
        }
        if (!StringUtils.isNullOrEmpty(method) && "view".equals(method)) {
            view(req, resp);
        }
        if (!StringUtils.isNullOrEmpty(method) && "delbill".equals(method)) {
            delBill(req, resp);
        }
        if (!StringUtils.isNullOrEmpty(method) && "modify".equals(method)) {
            modify(req, resp);
        }
        if (!StringUtils.isNullOrEmpty(method) && "modifysave".equals(method)) {
            modifySave(req, resp);
        }
    }

    private void modifySave(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        //获取前端数据
        String id = req.getParameter("id");
        String billCode = req.getParameter("billCode");
        String productName = req.getParameter("productName");
        String productUnit = req.getParameter("productUnit");
        String productCount = req.getParameter("productCount");
        String totalPrice = req.getParameter("totalPrice");
        String providerId = req.getParameter("providerId");
        String isPayment = req.getParameter("isPayment");
        System.out.println("id " + id);
        //传参
        Contact contact = new Contact();
        contact.setId(Integer.parseInt(id));
        contact.setBillCode(billCode);
        contact.setProductName(productName);
        contact.setProductUnit(productUnit);
        contact.setProductCount(new BigDecimal(productCount));
        contact.setTotalPrice(new BigDecimal(totalPrice));
        contact.setTotalPrice(new BigDecimal(providerId));
        contact.setIsPayment(Integer.parseInt(isPayment));
        contact.setModifyDate(new Date());
        contact.setModifyBy(((User)req.getSession().getAttribute(Constans.USER_SESSION)).getId());
        BillService billService = new BillServiceImpl();
        int result = billService.upDate(contact);
        if (result > 0) {
            resp.sendRedirect(req.getContextPath() + "/jsp/bill.do?method=query");
        }else {
            req.setAttribute("bill", contact);
            req.getRequestDispatcher("/jsp/bill/billmodify.jsp").forward(req, resp);
        }
    }

    private void modify(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        String billid = req.getParameter("billid");
        BillService billService = new BillServiceImpl();
        List<Contact> contact = billService.getContact(null, null, 0, Integer.parseInt(billid));
        req.setAttribute("bill", contact.get(0));
        req.getRequestDispatcher("/jsp/bill/billmodify.jsp").forward(req, resp);
    }

    private void delBill(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        String billid = req.getParameter("billid");
        BillService billService = new BillServiceImpl();
        Map<Object, Object> map = new HashMap<Object, Object>();
        if (Integer.parseInt(billid) <= 0) {
            map.put("delResult", "notexist");
        }else {
            int result = billService.del(billid);
            if (result > 0) {
                map.put("delResult", "true");
            }else {
                map.put("delResult", "false");
            }
        }
        //json
        PrintWriter writer = resp.getWriter();
        writer.write("application/json; charset = utf-8");
        writer.write(JSON.toJSONString(map));
        writer.flush();
        writer.close();
    }

    private void view(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        String billid = req.getParameter("billid");
        BillService billService = new BillServiceImpl();
        List<Contact> contact = billService.getContact(null, null, 0, Integer.parseInt(billid));
        req.setAttribute("bill", contact.get(0));

        req.getRequestDispatcher("/jsp/bill/billview.jsp").forward(req, resp);
    }

    private void getProviderList(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        ProviderService providerService = new ProviderServiceImpl();
        List<Provider> provider = providerService.getProvider(null, null, null, 0, 100);

        //JSON
        resp.setContentType("application/text; charset=utf-8");
        PrintWriter writer = resp.getWriter();
        writer.write(JSON.toJSONString(provider));
        writer.flush();
        writer.close();
    }

    private void add(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        //获取前端数据
        String billCode = req.getParameter("billCode");
        String productName = req.getParameter("productName");
        String productUnit = req.getParameter("productUnit");
        String productDesc = req.getParameter("productDesc");
        String productCount = req.getParameter("productCount");
        String totalPrice = req.getParameter("totalPrice");
        String providerId = req.getParameter("providerId");
        String isPayment = req.getParameter("isPayment");

        //传递参数
        Contact contact = new Contact();

        //赋值
        contact.setBillCode(billCode);
        contact.setProductName(productName);
        contact.setProductUnit(productUnit);
        contact.setProductDesc(productDesc);
        contact.setProductCount(new BigDecimal(productCount));
        contact.setTotalPrice(new BigDecimal(totalPrice));
        contact.setProviderId(Integer.parseInt(providerId));
        contact.setIsPayment(Integer.parseInt(isPayment));
        contact.setCreationDate(new Date());
        contact.setCreatedBy(((User)req.getSession().getAttribute(Constans.USER_SESSION)).getId());

        //调用servce
        BillService billService = new BillServiceImpl();
        int result = billService.add(contact);

        if (result > 0) {
            resp.sendRedirect(req.getContextPath() + "/jsp/bill.do?method=query");
        }else {
            req.getRequestDispatcher("/jsp/bill/billadd.jsp").forward(req, resp);
        }
    }

    private void query(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        BillService billService = new BillServiceImpl();

        //获取前端数据
        String queryProductName = req.getParameter("queryProductName");
        String queryProviderId = req.getParameter("queryProviderId");
        String queryIsPayment = req.getParameter("queryIsPayment");
        int isPay = 0;

        //获取供应商数据
        ProviderService providerService = new ProviderServiceImpl();
        List<Provider> provider = providerService.getProvider(null, null, null, 0, 100);
        req.setAttribute("providerList", provider);

        if(!StringUtils.isNullOrEmpty(queryIsPayment)) isPay = Integer.parseInt(queryIsPayment);
        List<Contact> billList = billService.getContact(queryProductName, queryProviderId, isPay, 0);

        req.setAttribute("queryProductName", queryProductName);
        req.setAttribute("queryIsPayment", queryIsPayment);
        req.setAttribute("queryProviderId", queryProviderId);
        req.setAttribute("billList", billList);
        req.getRequestDispatcher("/jsp/bill/billlist.jsp").forward(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
